`
https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var sumRootToLeaf = function (root) {
  let sum = 0
  const path = []

  const traverse = (root) => {
    if (root === null) {
      return
    }

    // 将当前值放入路径
    path.push(root.val)

    // 如果当前结点是叶子结点
    if (root.left === null && root.right === null) {
      // 将当前路径拼接为字符串，并转换为十进制数累加到结果
      sum += parseInt(path.join(''), 2)
    }

    // 递归遍历
    traverse(root.left)
    traverse(root.right)

    // 将当前值从路径里取出来
    path.pop()
  }

  traverse(root)

  return sum
};